package com.shift.sdk.view;

import java.util.LinkedList;
import java.util.List;
import com.shift.ccsdk.CCDetail;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class RectView extends View {
	
	Paint paint;
	
	int picWidth,picHeight;
	
	List<CCDetail> ddCcDetails = new LinkedList<CCDetail>();
	
	public static final int DISAPPEAR_TIME = 300;
	
	public RectView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		paint.setColor(Color.GREEN);
		paint.setStrokeWidth(5);
		paint.setAntiAlias(true);//去掉边缘锯齿
		paint.setStyle(Style.STROKE);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
//		System.out.println("width="+getLayoutParams().width+",height="+getLayoutParams().height);
//		System.out.println("picWidth="+picWidth+",picHeight="+picHeight);
		for(int i=0;i<ddCcDetails.size();i++){
			CCDetail ccDetail = ddCcDetails.get(i);
			Path path = new Path();
			path.moveTo(getScale(ccDetail.topLeft.X, true),getScale(ccDetail.topLeft.Y,false));
			path.lineTo(getScale(ccDetail.topRight.X,true), getScale(ccDetail.topRight.Y,false));
			path.lineTo(getScale(ccDetail.bottomRight.X,true), getScale(ccDetail.bottomRight.Y,false));
			path.lineTo(getScale(ccDetail.bottomLeft.X,true), getScale(ccDetail.bottomLeft.Y,false));
//			System.out.println("topLeft:("+ccDetail.topLeft.X+","+ccDetail.topLeft.Y+")");
//			System.out.println("topRight:("+ccDetail.topRight.X+","+ccDetail.topRight.Y+")");
//			System.out.println("bottomRight:("+ccDetail.bottomRight.X+","+ccDetail.bottomRight.Y+")");
//			System.out.println("bottomLeft:("+ccDetail.bottomLeft.X+","+ccDetail.bottomLeft.Y+")");
			path.close();
			canvas.drawPath(path, paint);
		}
	}
	
	private int getScale(long num,boolean width){
		if(width){
			return (int) (num*getLayoutParams().width/picWidth);
		}else{
			return (int)(num*getLayoutParams().height/picHeight);
		}
	}

	public List<CCDetail> getDdCcDetails() {
		return ddCcDetails;
	}
	
	public void setDdCcDetails(List<CCDetail> ddCcDetails) {
		this.ddCcDetails = ddCcDetails;
	}

	public int getPicWidth() {
		return picWidth;
	}

	public void setPicWidth(int picWidth) {
		this.picWidth = picWidth;
	}

	public int getPicHeight() {
		return picHeight;
	}

	public void setPicHeight(int picHeight) {
		this.picHeight = picHeight;
	}
}
